package com.java.util;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Map;
import java.util.HashMap;

/**
 * 遍历map，并且修改当前的value值
 */
public class MapReplace {

    public static void main(String[] args) {

        test2();
    }

    public static void test1() {
        Map<Long, BigDecimal> map = new HashMap<>();
        map.put(112L, new BigDecimal("123"));
        map.put(113L, new BigDecimal("42"));
        map.put(114L, new BigDecimal("66"));

        Map<Long, BigDecimal> map2 = new HashMap<>();
        map2.put(112L, new BigDecimal("3"));
        map2.put(113L, new BigDecimal("1"));
        map2.put(114L, new BigDecimal("2"));

        // 打印修改前的map
        map.forEach((key, value) -> System.out.println(key + ": " + value));
        System.out.println("-----------------");

        // 遍历map并修改value
        map.replaceAll((campaignUuid, sumCost) -> {

            System.out.println("processing campaignUuid: " + campaignUuid);
            return sumCost.add(map2.getOrDefault(campaignUuid, BigDecimal.ZERO));
        });

        // 打印修改后的map
        map.forEach((key, value) -> System.out.println(key + ": " + value));
    }

    public static void test2() {
        Map<Long, Map<Long, BigDecimal>> map = new HashMap<>();
        Map<Long, BigDecimal> innerMap1 = new HashMap<>();
        innerMap1.put(112L, new BigDecimal("123"));

        Map<Long, BigDecimal> innerMap2 = new HashMap<>();
        innerMap2.put(113L, new BigDecimal("42"));

        Map<Long, BigDecimal> innerMap3 = new HashMap<>();
        innerMap3.put(114L, new BigDecimal("66"));

        map.put(1111111111111111111L, innerMap1);

        map.put(2222222222222222222L, innerMap2);

        map.put(3333333333333333333L, innerMap3);
        map.forEach((key, value) -> System.out.println(key + ": " + value));
        System.out.println("-----------------");



        Map<Long, Map<Long, BigDecimal>> map2 = new HashMap<>();
        Map<Long, BigDecimal> innerMap11 = new HashMap<>();
        innerMap11.put(112L, new BigDecimal("1"));

        Map<Long, BigDecimal> innerMap22 = new HashMap<>();
        innerMap22.put(113L, new BigDecimal("1"));

        Map<Long, BigDecimal> innerMap33 = new HashMap<>();
        innerMap33.put(114L, new BigDecimal("1"));

        map2.put(1111111111111111111L, innerMap11);

        map2.put(2222222222222222222L, innerMap22);

        map2.put(3333333333333333333L, innerMap33);

        map.forEach((campaignUuid, hotelCostMap) -> {
            Map<Long, BigDecimal> avgCostMap = map2.getOrDefault(campaignUuid, Collections.emptyMap());
            hotelCostMap.replaceAll((hotelId, cost) ->
                    cost.add(avgCostMap.getOrDefault(hotelId, BigDecimal.ZERO))
            );
        });

        map.forEach((key, value) -> System.out.println(key + ": " + value));
    }
}
